//
//  CollectionViewController.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/18.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "CollectionViewController.h"

@interface CollectionViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray *dataSource;
@property (nonatomic, strong)CoreDataManager *manager;
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //打印路径
    self.manager = [CoreDataManager shareManager];
    NSLog(@"%@", [self.manager applicationDocumentsDirectory]);
    
//    [self selectAllData];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 49) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CollectionTableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    //查询所有
    [self selectAllData];
    [self.tableView reloadData];
}



//点击跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AlbumViewController *albumVC = [[AlbumViewController alloc] init];
    albumVC.hotRecommendListModel = [[HotRecommendListModel alloc] init];
    
    

    AlbumList *albumList = self.dataSource[indexPath.row];
    albumVC.hotRecommendListModel.albumId = albumList.url;
    [self.navigationController pushViewController:albumVC animated:YES];
    
}


- (void)selectAllData {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    //设置要请求的实体
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"AlbumList" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    self.dataSource = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
//    self.dataSource = [NSMutableArray arrayWithArray:[self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error]];
    
    
    if (self.dataSource == nil) {
        NSLog(@"%@", error);
    }
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AlbumList *albumList = self.dataSource[indexPath.row];
    
    CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
   
    cell.albumList = albumList;
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

//滑动删除

//编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//删除事件

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.manager.managedObjectContext deleteObject:self.dataSource[indexPath.row]];

    [self.manager saveContext];

    [self selectAllData];
    
    
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除本条";
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
